package com.ruoyi.admin.service.impl;

import com.ruoyi.admin.service.TravelRouteService;
import com.ruoyi.app.domain.*;
import com.ruoyi.app.mapper.TravelRouteDetailMapper;
import com.ruoyi.app.mapper.TravelRouteMapper;
import com.ruoyi.app.mapper.TravelRouteStatsMapper;
import com.ruoyi.app.mapper.TravelRouteSwiperMapper;
import com.ruoyi.framework.web.domain.AjaxResult;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/*
@description:
@ClassName TravelRouteServiceImpl
@author chen
@create 2025-04-06 2:18
@Version 1.0
*/
@Slf4j
@Service
@AllArgsConstructor
public class TravelRouteServiceImpl implements TravelRouteService
{
    final TravelRouteMapper travelRouteMapper;
    final TravelRouteDetailMapper travelRouteDetailMapper;
    final TravelRouteStatsMapper travelRouteStatsMapper;

    final TravelRouteSwiperMapper travelRouteSwiperMapper;

    @Transactional
    @Override
    public AjaxResult insertTravelRouteInfo(TravelRoute travelRoute)
    {
        travelRoute.setCreateTime(new Date());
        travelRoute.setUpdateTime(new Date());
        travelRouteMapper.insertSelective(travelRoute);

        List<TravelRouteDetail> detailItems = travelRoute.getDetailItems();
        if(null != detailItems)
        {
            for (TravelRouteDetail detailItem : detailItems)
            {
                detailItem.setRouteId(travelRoute.getId());
                detailItem.setCreateTime(new Date());
                detailItem.setUpdateTime(new Date());
                travelRouteDetailMapper.insertSelective(detailItem);
            }
        }
        List<TravelRouteStats> stats = travelRoute.getStats();
        if(null != stats)
        {
            for (TravelRouteStats stat : stats)
            {
                stat.setRouteId(travelRoute.getId());
                stat.setCreateTime(new Date());
                stat.setUpdateTime(new Date());
                travelRouteStatsMapper.insertSelective(stat);
            }
        }
        List<TravelRouteSwiper> swiperList = travelRoute.getSwiperList();
        if(null != swiperList)
        {
            for (TravelRouteSwiper travelRouteSwiper : swiperList)
            {
                travelRouteSwiper.setRouteId(travelRoute.getId());
                travelRouteSwiper.setCreateTime(new Date());
                travelRouteSwiper.setUpdateTime(new Date());
                travelRouteSwiperMapper.insertSelective(travelRouteSwiper);
            }
        }
        return AjaxResult.success();
    }

    /*
     * @==============History===============<br/>
     * @Description //   获取路线列表
     * @Date 2025-4-6 9:23
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return java.util.List<com.ruoyi.app.domain.TravelRoute>
     */
    @Override
    public List<TravelRoute> getTravelRouteList(Map<String, Object> params)
    {
        List<TravelRoute> travelRoutes = travelRouteMapper.selectByExample(null);
        if(null != travelRoutes)
        {
            for (TravelRoute travelRoute : travelRoutes)
            {
                TravelRouteSwiperExample travelRouteSwiperExample = new TravelRouteSwiperExample();
                travelRouteSwiperExample.createCriteria().andRouteIdEqualTo(travelRoute.getId());
                List<TravelRouteSwiper> travelRouteSwiperList = travelRouteSwiperMapper.selectByExample(travelRouteSwiperExample);
                travelRoute.setSwiperList(travelRouteSwiperList);
            }
        }
        return travelRoutes;
    }

    @Override
    public AjaxResult getTravelRouteById(Long id)
    {
        TravelRoute travelRoute = travelRouteMapper.selectByPrimaryKey(id);
        TravelRouteSwiperExample travelRouteSwiperExample = new TravelRouteSwiperExample();
        travelRouteSwiperExample.createCriteria().andRouteIdEqualTo(id);
        List<TravelRouteSwiper> travelRouteSwiperList = travelRouteSwiperMapper.selectByExample(travelRouteSwiperExample);
        travelRoute.setSwiperList(travelRouteSwiperList);
        TravelRouteStatsExample travelRouteStatsExample = new TravelRouteStatsExample();
        travelRouteStatsExample.createCriteria().andRouteIdEqualTo(id);
        List<TravelRouteStats> travelRouteStats = travelRouteStatsMapper.selectByExample(travelRouteStatsExample);
        travelRoute.setStats(travelRouteStats);
        TravelRouteDetailExample travelRouteDetailExample = new TravelRouteDetailExample();
        travelRouteDetailExample.createCriteria().andRouteIdEqualTo(id);
        List<TravelRouteDetail> travelRouteDetails = travelRouteDetailMapper.selectByExample(travelRouteDetailExample);
        travelRoute.setDetailItems(travelRouteDetails);
        return AjaxResult.success(travelRoute);
    }

    @Override
    public AjaxResult deleteRouteById(Long id)
    {
        return travelRouteMapper.deleteByPrimaryKey(id) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Transactional
    @Override
    public AjaxResult updateTravelRouteInfo(TravelRoute travelRoute)
    {
        travelRouteMapper.deleteByPrimaryKey(travelRoute.getId());
        travelRoute.setId(null);
        insertTravelRouteInfo(travelRoute);
        return AjaxResult.success();
    }


}
